package com.example.apple.printingroommanagementsystem;

import android.content.Context;
import android.support.v4.view.ViewCompat;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;

/**
 * Created by apple on 17/3/24.
 */

public class SwipeLayout extends FrameLayout{

    private View contentView;//内容view
    private View deleteView;//操作view
    private int deleteWidth;//删除按钮的宽
    private int deleteHeight;//删除按钮的高
    private int contentWidth;
    private ViewDragHelper viewDragHelper;

    public SwipeLayout(Context context) {
        super(context);
        init();
    }

    public SwipeLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SwipeLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    //设置布局的回调
    private  void init(){
        viewDragHelper  = ViewDragHelper.create(this,callback);
    }
    //当加载完毕时，获取它的子视图
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
         contentView = getChildAt(0);
         deleteView = getChildAt(1);
    }

    //在这个方法中可以获取布局的宽高
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
         deleteHeight= deleteView.getMeasuredHeight();
         deleteWidth = deleteView.getMeasuredWidth();
         contentWidth = contentView.getMeasuredWidth();
    }
    //设置布局
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        contentView.layout(0,0,contentWidth,deleteHeight);
        deleteView.layout(contentView.getRight(),0,contentView.getRight()+deleteWidth,deleteHeight);
    }
    //是否把事件传递（可以拦截不向下传递，也可以向下传递）
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean result = viewDragHelper.shouldInterceptTouchEvent(ev);
        return result ;
    }
    //点击事件
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        viewDragHelper.processTouchEvent(event);
        return true;
    }



    //回调方法
    private  ViewDragHelper.Callback callback =new ViewDragHelper.Callback() {
        //捕获滑动的视图
        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return  child ==contentView||child==deleteView;
        }

       // 设置水平方向滑动范围
        @Override
        public int getViewHorizontalDragRange(View child) {
             return deleteWidth;
        }
        //判断滑动区域，并设置
        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            if (child==contentView){
                if (left>0)left=0;
                if (left<-deleteWidth)left =-deleteWidth;
            } else if (child==deleteView){
                if (left>contentWidth)left =contentWidth;
                if (left<(contentWidth-deleteWidth))left =contentWidth-deleteWidth;
            }
            return left;
        }

        //设置跟随移动
        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
         if (changedView ==contentView){
             //手动移动deleteview
             deleteView.layout(deleteView.getLeft()+dx,deleteView.getTop()+dy,deleteView.getRight()+dx,deleteView.getBottom()+dy);
         }
            if (changedView==deleteView){
                contentView.layout(contentView.getLeft()+dx,contentView.getTop()+dy,contentView.getRight()+dx,contentView.getBottom()+dy);
            }
        }

        //判断是否打开（放手时）
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            if (contentView.getLeft()<-deleteWidth/2){
                //打开状态
                open(contentView, -deleteWidth, contentView.getTop());
            }else {
                //关闭状态
                close(contentView,0, contentView.getTop());
            }
        }
    };
    //关闭方法
    private void close(View contentView,int finalLeft, int top) {
        viewDragHelper.smoothSlideViewTo(contentView, finalLeft, top);
        ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
    }
    //打开方法
    private void open(View contentView, int finalLeft, int top) {
        viewDragHelper.smoothSlideViewTo(contentView, finalLeft, top);
        ViewCompat.postInvalidateOnAnimation(SwipeLayout.this);
    }


    @Override
    public void computeScroll() {
        Log.e("--->1","");
        if (viewDragHelper.continueSettling(true));
        ViewCompat.postInvalidateOnAnimation(this);
    }
}
